package com.bwsoft.action;

import com.bwsoft.domain.DictionaryData;
import com.bwsoft.model.EasyTree;
import org.apache.log4j.Logger;

import java.util.List;

public class DictionaryAction extends BaseAction {

	private static final long serialVersionUID = 2224654913714342148L;
	private static Logger LOG = Logger.getLogger(DictionaryAction.class);
	
	public String index(){
		return "index";
	}
	
	
	/**
	 * 初始化树
	 */
	public String getDicTreeJson() {
		List<EasyTree> treeList = dictionaryService.findAllByTree();
		responseJson.put("id", 0);
		responseJson.put("text", "字典树");
		responseJson.put("state", "open");
		responseJson.put("iconCls", "icon-book_addresses");
		responseJson.put("children", treeList);
		responseListJson.add(responseJson);
		return "listJsonResult";
	}
	
	
	/**
	 * 通过点击左侧树获取右侧列表
	 */
	public String getDictionaryDataJson() {

		int count = 0;
		List<DictionaryData> dictionaryDataSet=null;
		
		if (dictionary != null) {
			
			//通过字典编号查询字典详情的分页集合
			dictionaryDataSet = dictionaryService.findByPage(page, rows,dictionary);
			//通过编号查询字典集合的大小	
			count = dictionaryService.getTotalCount(dictionary);
		}
		
		responseJson.put("total", count);
		responseJson.put("rows", dictionaryDataSet);

		return "mapJsonResult";
	}
	
	
	/**
	 * 字典保存或新增
	 * @return
	 */
	public String saveOrUpdateDictionary(){
		
		msg = dictionaryService.saveOrUpdateDictionary(dictionary);
		
		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());
		return "mapJsonResult";
	}
	
	
	/**
	 * 字典保存或新增
	 * @return
	 */
	public String saveOrUpdateDictionaryData(){
		
		msg = dictionaryService.saveOrUpdateDictionaryData(dictionaryData);
		
		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());
		return "mapJsonResult";
	}
	
	
	/**
	 * 添加字典项的验证
	 * @return
	 */
	public String checkDictionaryInput(){
		
		msg = dictionaryService.checkDictionaryInput(dictionary);
		
		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());
		return "mapJsonResult";
	}
	
	
	/**
	 * 字典详情验证表单
	 * @return
	 */
	public String checkDictionaryDataInput(){
		
		msg = dictionaryService.checkDictionaryDataInput(dictionaryData);
		
		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());
		return "mapJsonResult";
	}
	
	/**
	 * 修改菜单树，获取该菜单的详细内容
	 * @return
	 */
	public String dictionaryModifyInfo(){
		dictionary=dictionaryService.dictionaryModifyInfo(dictionary.getDicId());
		responseJson.put("dictionary", dictionary);
		return "mapJsonResult";
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String removeDictionary(){
		msg = dictionaryService.removeDictionary(dictionary.getDicId());
		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());
		return "mapJsonResult";
		
	}
	/**
	 * 删除
	 * @return
	 */
	public String removeDictionaryData(){
		msg = dictionaryService.removeDictionaryData(dictionaryData.getDicDataId());
		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());
		return "mapJsonResult";
		
	}
	
	/**
	 * 获取字典详情内容
	 * @return
	 */
	public String dictionaryDataModifyInfo(){
		dictionaryData=dictionaryService.dictionaryDataModifyInfo(dictionaryData.getDicDataId());
		responseJson.put("dictionaryData", dictionaryData);
		return "mapJsonResult";
	}
	
}
